﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DataLib.DBUtility;
using System.Collections;

namespace DataLib.DAL
{
	/// <summary>
	/// 数据访问类:BarCode
	/// </summary>
	public partial class BarCode
	{
	
     

		#region  ExtensionMethod

        /// <summary>
        /// 是否存在该记录，存在则返回第一条
        /// </summary>
        public Model.BarCode BarCodeExists(string barcodevalue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from BarCode");
            strSql.Append(" where barcodevalue=@barcodevalue");
            SqlParameter[] parameters = {
					new SqlParameter("@barcodevalue", SqlDbType.VarChar,40)
			};
            parameters[0].Value = barcodevalue;

            DataTable dt = DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
                return DataRowToModel(dt.Rows[0]);
            else
                return null;
        }

        /// <summary>
        /// 根据barcode返回Model
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public DataLib.Model.BarCode GetModelByCode(string barcode)
        {
            DataTable dt = GetList("barcodevalue='" + barcode + "'").Tables[0];
            Model.BarCode model = new Model.BarCode();
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                model = DataRowToModel(dr);
            }
            return model;
            
        }

        /// <summary>
        /// 根据来源的出库单ID返回已经入过库的入库ID号
        /// </summary>
        /// <param name="outid">出库单的ID作为Barcode的Parmentid</param>
        /// <returns></returns>
        public int GetInStroageIDByParmentid(string outid)
        {
            string where = "";
            where = "ParmentID= " + outid;
            int insid = 0;
            DataTable dt = GetList(where).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                insid = int.Parse(dt.Rows[0]["InStrorageID"].ToString());
            }
            return insid;
        }

        /// <summary>
        /// 根据CJOID返回称重用的入库ID
        /// </summary>
        /// <param name="outid">出库单的ID作为Barcode的Parmentid</param>
        /// <returns></returns>
        public int GetInStroageIDByCjoid(string cjoid)
        {
            InStorage dalins = new InStorage();
            string where = "";
            where = "outid=0 and cjoid= " + cjoid;
            int insid = 0;
            DataTable dt = dalins.GetList(where).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                insid = int.Parse(dt.Rows[0]["id"].ToString());
            }
            return insid;

        }
        /// <summary>
        /// 统一修改BarCode的入库状态
        /// </summary>
        /// <param name="instorageid">入库ID号</param>
        /// <returns>是否修改成功</returns>
        public bool UpdateByInsID(int instorageid)
        {
            bool flag = false;
            DataTable dt = new DataTable();
            string sql = "select * from BarCode where status=-1 and isdel_out<>-1 and InStrorageID=" + instorageid.ToString();
            dt = DbHelperSQL.Query(sql).Tables[0];
            OutStorage oStorage = new OutStorage();
            Model.OutStorage oModel = new Model.OutStorage();
            decimal currweight = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Model.BarCode bmodel = DataRowToModel(dt.Rows[i]);

                    /**/
                    oModel = oStorage.GetModel((int)bmodel.ParmentID);

                    if (bmodel.ParmentID != 0 && oModel != null)
                    {
                        switch (oModel.outStorageType)
                        {
                            case "2":
                                bmodel.status = 3;
                                break;
                            case "4":
                                bmodel.status = 5;
                                break;
                            case "6":
                                bmodel.status = 7;
                                break;
                            case "8":
                                bmodel.status = 9;
                                break;

                            default:
                                bmodel.status = 1;
                                break;
                        }
                        currweight += (decimal)bmodel.nwt;
                       
                    }
                    else
                    {
                        bmodel.status = 1;
                    }

                    flag = Update(bmodel);
                    
                }
            }
        return flag;
        }

        /// <summary>
        /// 提交时有重复BarCode的处理
        /// </summary>
        /// <param name="barModel"></param>
        private bool SetModel(Model.BarCode barModel)
        {
            bool flag = false;
            DataTable dt1 = GetList("barcodevalue='" + barModel.barcodevalue + "' and OutStrorageID IS Not NULL ").Tables[0];
            Model.BarCode outBarCode = DataRowToModel(dt1.Rows[0]);

            Model.InStorage inModel = new InStorage().GetModel((int)outBarCode.InStrorageID);
            Model.OutStorage outModel = new OutStorage().GetModel((int)outBarCode.OutStrorageID);
            Model.StoragePostion spModel = new StoragePostion().GetModel((int)barModel.storageid);

            if (dt1 != null && dt1.Rows.Count > 0)
            {
                flag = true;
              

                barModel.status += 1;

                

                //插入一条出库记录
                Model.StorageData sdModel = new Model.StorageData();
                sdModel.goodsid = new Goods().GetgoodsID(barModel.goodscode);
           
                sdModel.goodscode =barModel.goodscode;
                sdModel.goodsName = barModel.goodsname;
                sdModel.barcodeid = barModel.id;
                sdModel.InOrOut = 2;
                sdModel.inoutid = barModel.OutStrorageID;
                sdModel.isused = true;
                sdModel.nwt = barModel.nwt;
                sdModel.operDate = DateTime.Now;
                sdModel.operman = inModel.Operator;
                sdModel.startNwt = new StorageData().CurEndNwt(sdModel.goodscode, sdModel.storageno);    //期初净重 
                sdModel.storageid = (int)barModel.storageid;
                sdModel.storageno = sdModel.storageno;
                sdModel.endNwt = sdModel.startNwt + sdModel.nwt;



                if (outModel != null)
                {
                    //出库单重量完成

                    outModel.currentWeight += sdModel.nwt;
                    if (outModel.totalWeight == outModel.currentWeight)
                    {
                        outModel.status = 0;        //0为出库单入库重量出完成
                    }
                    if (outModel.totalWeight < outModel.currentWeight)
                    {
                        flag = false;
                        return flag;
                    }
                    flag = new OutStorage().Update(outModel);
                }

                sdModel.id = new StorageData().Add(sdModel);

                spModel = new StoragePostion().GetModel(sdModel.storageid);
                spModel.currweight -= sdModel.nwt;
                spModel.currvalume += barModel.qty;
                new StoragePostion().Update(spModel); 


                
            }
            return flag;
        }

        /// <summary>
        /// 更改状态为入临时库
        /// </summary>
        /// <param name="instorageid">入库单外键</param>
        /// <returns></returns>
        public bool UpdateByWrl(int instorageid)
        {
            bool flag = false;
            DataTable dt = new DataTable();
            string sql = "select * from BarCode where InStrorageID=" + instorageid.ToString();
            dt = DbHelperSQL.Query(sql).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Model.BarCode bmodel = DataRowToModel(dt.Rows[i]);
                    bmodel.status = 1;  //入临时库
                    flag = Update(bmodel);

                }
            }
            return flag;
        }


        /// <summary>
        /// 根据临时库出库编号ID获取第一个符合的loadnumid
        /// </summary>
        /// <param name="outid"></param>
        /// <returns></returns>
        public int GetLoadIDByOutID(int outid)
        {
            //应该是status=2 临时库出库
            DataTable dt = new DataTable();
            int loadnumid = 0;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,loadnumid ");
            strSql.Append(" FROM BarCode ");
            if (outid != 0)
            {
                strSql.Append(" where OutStrorageID=" + outid.ToString());
            }
            else
            {
                strSql.Append(" where status=2");
            }
             dt = DbHelperSQL.Query(strSql.ToString()).Tables[0];
             if (dt.Rows.Count > 0)
             {
                 loadnumid = int.Parse(dt.Rows[0]["loadnumid"].ToString());
             }
          
            return loadnumid;
        }

        /// <summary>
        /// LoadNum单的总重量及已入成品库总量计算
        /// </summary>
        /// <param name="lodnumid"></param>
        /// <param name="status">1为临时库入库，计总重，3为分类入库，当前完成重量</param>
        /// <returns></returns>
        public bool UpdateLoadWeight(int lodnumid, string status)
        {
            bool flag = true;
            if (lodnumid != 0)
            {
                Model.LoadNum loadnum = new LoadNum().GetModel(lodnumid);
                decimal tempWeight = 0;
                DataTable dt = GetList("loadnumid=" + lodnumid.ToString() + " and status=-1").Tables[0];
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        tempWeight = decimal.Parse(dt.Rows[i]["nwt"].ToString());
                        if (status == "1")
                        {
                            loadnum.totalWeight += tempWeight;
                           
                        }
                        else if (status == "3")//分类入库即为完成
                        {
                            loadnum.finishWeight += tempWeight;
                            if (loadnum.finishWeight == loadnum.totalWeight)
                                loadnum.status = 1;         //Load单完成
                            else if (loadnum.finishWeight > loadnum.totalWeight)
                            {
                                loadnum.status = -1;        //入库重量超过
                                flag = false;
                            }
                        }
                    }
                }
                flag = new LoadNum().Update(loadnum);
            }
            return flag;
        }

        /// <summary>
        /// 更新库存及出库单状态
        /// </summary>
        /// <param name="inStoID">入库编号</param>
        /// <returns></returns>
        public bool UpdateStoragePostion(int inStoID,string oper)
        {
            //库存
            bool flag = false;
            Model.InStorage inModel = new Model.InStorage();
            Model.OutStorage outModel = new Model.OutStorage();
            Model.StoragePostion spModel = new Model.StoragePostion();
            
            InStorage inDal = new InStorage();
            OutStorage outDal = new OutStorage();
            StoragePostion spDal = new StoragePostion();

            inModel = inDal.GetModel(inStoID);
            if(inModel.outid!=0)
                outModel = outDal.GetModel((int)inModel.outid);
            else
                outModel = null;

            //取出入库单号下的所有记录
            string strsql = "SELECT *  FROM BarCode where status=-1 and InStrorageID = " + inStoID.ToString();
            DataTable pdt = new DataTable();    //barcode列表
            pdt = DbHelperSQL.Query(strsql).Tables[0];
            
            //Hashtable ht = new Hashtable();
            if (pdt != null && pdt.Rows.Count > 0)
            {
                for (int i = 0; i < pdt.Rows.Count; i++)
                {
                    //插入库存操作记录
                 Model.StorageData sdmodel = new Model.StorageData();
                 sdmodel.goodsid = new Goods().GetgoodsID(pdt.Rows[i]["goodscode"].ToString());
                 sdmodel.goodscode = pdt.Rows[i]["goodscode"].ToString();
                 sdmodel.goodsName = pdt.Rows[i]["goodsname"].ToString();
                 sdmodel.barcodeid = int.Parse(pdt.Rows[i]["id"].ToString());
                 sdmodel.InOrOut = 1;
                 sdmodel.inoutid = inStoID;
                 sdmodel.isused = true;
                 sdmodel.nwt = decimal.Parse(pdt.Rows[i]["nwt"].ToString());
                 sdmodel.operDate = DateTime.Now;
                 sdmodel.operman = oper;
                 sdmodel.startNwt = new StorageData().CurEndNwt(sdmodel.goodscode, pdt.Rows[i]["stroageNo"].ToString());    //期初净重 
                 sdmodel.storageid = int.Parse(pdt.Rows[i]["storageid"].ToString());
                 sdmodel.storageno = pdt.Rows[i]["stroageNo"].ToString();
                 sdmodel.endNwt = sdmodel.startNwt + sdmodel.nwt;

          
                  
                    if(outModel != null)//出库单不为空，还有可能是出库分类，这里只计算出库单
                    {
                            //出库单重量完成

                            outModel.currentWeight += sdmodel.nwt;
                            if (outModel.totalWeight  == outModel.currentWeight)
                            {
                                outModel.status = 0;        //0为出库单入库重量出完成
                            }
                            if (outModel.totalWeight < outModel.currentWeight)
                            {
                                flag = false;
                                return flag;
                            }
                            flag = new OutStorage().Update(outModel);
                    }

                    sdmodel.id = new StorageData().Add(sdmodel);

                    spModel = spDal.GetModel(sdmodel.storageid);
                    spModel.currweight += sdmodel.nwt;
                    spModel.currvalume += int.Parse(pdt.Rows[i]["qty"].ToString().Trim());
                    spDal.Update(spModel);
                    flag = true;
                   
                }
            }

            return flag;
        }

        /// <summary>
        /// 物品明细
        /// </summary>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="strbelong">仓库所属城市</param>
        /// <param name="strdis">仓库地理位置</param>
        /// <param name="gcode">物品编码</param>
        /// <param name="stroageno">库位号</param>
        /// <param name="flag">明细类型</param>
        /// <returns></returns>
        public DataSet GetDsByWhere(DateTime startdate, DateTime enddate, string strbelong, string strdis, string gcode, string stroageno,int flag)
        {
            DataTable dt = new DataTable();
            SqlParameter[] sqlparameter ={
                new SqlParameter("@BelongCity",SqlDbType.VarChar,30),
                new SqlParameter("@GraphiCity",SqlDbType.NVarChar,30),
                new SqlParameter("@StartDate",SqlDbType.NVarChar,30),
                new SqlParameter("@EndDate",SqlDbType.NVarChar,30),
                new SqlParameter("@goodsCode",SqlDbType.NVarChar,30),
                new SqlParameter("@StorageNo",SqlDbType.NVarChar,30),
                new SqlParameter("@flag",SqlDbType.Int)

             };
            sqlparameter[0].Value = strbelong;
            sqlparameter[1].Value = strdis;
            sqlparameter[2].Value = startdate;
            sqlparameter[3].Value = enddate;
            sqlparameter[4].Value = gcode;
            sqlparameter[5].Value = stroageno;
            sqlparameter[6].Value = flag;
            SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_GetStorageItems", sqlparameter);
            dt.Load(dr);
            dr.Close();
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            return ds;
        }
        /// <summary>
        /// 根据入库单ID获取
        /// </summary>
        /// <param name="instroageid">入库单id</param>
        /// <returns></returns>
        public DataSet GetCountList(string instroageid)
        {
            DataSet ds = new DataSet();
            string strsql = @"SELECT barcodevalue,sum(nwt) nwt,SUM(gwt) gwt, SUM(qty) qty,
	SUM(tare1) tare1,SUM(tare2) tare2,MAX(id) id,Max(loadnumid) loadnumid,
	max(goodscode) goodscode,MAX(goodsname) goodsname,
	MAX(remark) remark,MAX(status) inorout,COUNT(id) bacount,MAX(InStrorageID) instrorageid,
	max(OutStrorageID) OutStrorageID,MAX(stroageNo) stroageNo, MAX(rdh) rdh,
	MAX(rubbishcode) rubbishcode, MAX(rubbishtype) rubbishtype, MAX(plateno) plateno
  FROM BarCode
  where InStrorageID=";
            strsql += instroageid.ToString() + " group by barcodevalue";
            ds = DbHelperSQL.Query(strsql);
            return ds;
        }

        /// <summary>
        /// 获取组合后的barcode
        /// </summary>
        /// <param name="instroageid">入库单id</param>
        /// <returns></returns>
        public DataSet GetBarList(string where)
        {
            DataSet ds = new DataSet();
            string strsql = @"SELECT barcodevalue,sum(nwt) nwt,SUM(gwt) gwt, SUM(qty) qty,
	SUM(tare1) tare1,SUM(tare2) tare2,MAX(id) id,Max(loadnumid) loadnumid,
	max(goodscode) goodscode,MAX(goodsname) goodsname,
	MAX(remark) remark,MAX(status) inorout,COUNT(id) bacount,MAX(InStrorageID) instrorageid,
	max(OutStrorageID) OutStrorageID,MAX(stroageNo) stroageNo, MAX(rdh) rdh,
	MAX(rubbishcode) rubbishcode, MAX(rubbishtype) rubbishtype, MAX(plateno) plateno
  FROM BarCode ";
            if (where.Trim() != "")
            {
                strsql = strsql + " where " + where;
            }
            strsql = strsql + "  group by barcodevalue";
            ds = DbHelperSQL.Query(strsql);
            return ds;
        }

        /// <summary>
        /// 合成BarCode
        /// </summary>
        /// <param name="barcodeValue"></param>
        /// <returns></returns>
        public Model.BarCode GetByValue(string barcodeValue)
        {
            Model.BarCode model = new Model.BarCode();
            DataTable dt = new DataTable();
            SqlParameter[] sqlparameter ={
                new SqlParameter("@barcode",SqlDbType.VarChar,35)
             };
            sqlparameter[0].Value = barcodeValue;
            SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_GetBarCode", sqlparameter);
            dt.Load(dr);
            dr.Close();
            if (dt != null && dt.Rows.Count > 0)
                model = DataRowToModel(dt.Rows[0]);
            else
                model = null;
            return model;
        }


        /// <summary>
        /// 根据barcode的ID数组返回barcodevalue数组
        /// </summary>
        /// <param name="strid"></param>
        /// <returns></returns>
        public DataTable GetBarCodeValue(string strid)
        {
            string strsql = "select barcodevalue from BarCode where id in (" + strid + ")";
            DataTable dt = new DataTable();
            dt = DbHelperSQL.Query(strsql).Tables[0];
            return dt;
        }

 

        /// <summary>
        /// 获取城市编码
        /// </summary>
        /// <param name="cityname"></param>
        /// <returns></returns>
        public string GetCityCode(string cityname)
        {
            string citycode = "00";
            switch(cityname)
            {
                case "上海":
                    citycode = "01";
                    break;
                case "北京":
                    citycode = "02";
                    break;
                case "苏州":
                    citycode = "03";
                    break;
                case "广州":
                    citycode = "04";
                    break;
                default:
                    break;

            }
            return citycode;
        }

         /// <summary>
        /// 获取最新的库存各数据编号
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private string GetStartStorageNo(int type,string left)
        {
            string strno = "";
            int totallen = 0;
            string tabname = "";
            string fieldname = "";
            switch (type)
            { 
                case 1:             //Load号
                    totallen = 9;
                    tabname = "OutStorage";
                    fieldname = "VLoadNum";
                    break;
                case 2:             //入库单号
                    totallen = 7;
                    tabname = "Instorage";
                    fieldname = "wrlno";
                    break;
                case 3:             //BarCode号
                    totallen = 30;
                    tabname = "BarCode";
                    fieldname = "barcodevalue";
                    break;
                default:
                    break;
            }
            strno = DbHelperSQL.GetDataMaxNo(left, totallen, fieldname, tabname);
            return strno;
        }

  
       /// <summary>
        /// 获取最新条码编号
        /// </summary>
        /// <param name="LoadNo">虚拟Load号</param>
         /// <param name="GoodsCode">物品编码</param>
         /// <param name="belongcity">归属地</param>
         /// <param name="graphicity">处理地</param>
        /// <returns></returns>
        public string GetBarCode(string LoadNo, string GoodsCode,string belongcity, string graphicity)
        {
            string insno = "RP";
            insno = insno + GetCityCode(belongcity) + GetCityCode(graphicity);
            string left;
            left = LoadNo + insno + GoodsCode.PadRight(10, '0').Substring(0,10);
            return GetStartStorageNo(3, left);
        }

        /// <summary>
        /// 返回BarCode
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Model.BarCode InsertBarcode(Model.BarCode model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@loadid", SqlDbType.Int,4),
					new SqlParameter("@cjoid", SqlDbType.Int,4),
					new SqlParameter("@insid", SqlDbType.Int,4),
					new SqlParameter("@belongcity", SqlDbType.NVarChar,30),
					new SqlParameter("@graphicity", SqlDbType.NVarChar,35),
					new SqlParameter("@Date_PD", SqlDbType.NVarChar,20),
					new SqlParameter("@GoodsCode", SqlDbType.NVarChar,30),
					new SqlParameter("@GoodsName", SqlDbType.NVarChar,50),
					new SqlParameter("@GoodsID", SqlDbType.Int,4),
					new SqlParameter("@gwt", SqlDbType.Decimal,9),
					new SqlParameter("@tare1", SqlDbType.Decimal,9),
					new SqlParameter("@tare2", SqlDbType.Decimal,9),
					new SqlParameter("@qty", SqlDbType.Int,4),
					new SqlParameter("@storageid", SqlDbType.Int,4),
					new SqlParameter("@storageno", SqlDbType.NVarChar,30),
					new SqlParameter("@barcodevalue", SqlDbType.NVarChar,35),
					new SqlParameter("@barid", SqlDbType.Int,4),
                    new SqlParameter("@month", SqlDbType.NVarChar,20),
                    new SqlParameter("@belongcode", SqlDbType.NVarChar,4),
                    new SqlParameter("@graphiccode", SqlDbType.NVarChar,4),

                    new SqlParameter("@rcode", SqlDbType.NVarChar,40),
                    new SqlParameter("@rtype", SqlDbType.NVarChar,40),
					new SqlParameter("@boxno", SqlDbType.NVarChar,35),
                    new SqlParameter("@boxcount", SqlDbType.Int,4),
                    new SqlParameter("@rdh", SqlDbType.NVarChar,20),
					new SqlParameter("@remark", SqlDbType.NVarChar,80)
                                        };
            parameters[0].Value = model.loadnumid;
            parameters[1].Value = model.cjoid;
            parameters[2].Value = model.InStrorageID;
            parameters[3].Value = model.barbelong;
            parameters[4].Value = model.bargraphic;
            parameters[5].Value = model.createdate.ToString();
            parameters[6].Value = model.goodscode;
            parameters[7].Value = model.goodsname;
            parameters[8].Value = model.goodsid;
            parameters[9].Value = model.gwt;
            parameters[10].Value = model.tare1;
            parameters[11].Value = model.tare2;
            parameters[12].Value = model.qty;
            parameters[13].Value = model.storageid;
            parameters[14].Value = model.stroageNo;
            parameters[15].Value = model.barcodevalue;
            parameters[16].Value = model.id;
            parameters[17].Value = model.storagetype;       //结算月临时调用
            parameters[18].Value = GetCityCode(model.barbelong);
            parameters[19].Value = GetCityCode(model.bargraphic);
            parameters[20].Value = model.rubbishcode;
            parameters[21].Value = model.rubbishtype;
            parameters[22].Value = model.packageno;
            parameters[23].Value = model.boxcount;
            parameters[24].Value = model.rdh;
            parameters[25].Value = model.remark;
            DataTable dt = new DataTable();
            
            SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_PDRK", parameters);
            dt.Load(dr);
            dr.Close();

            if (dt != null && dt.Rows.Count > 0)
            {
                model = DataRowToModel(dt.Rows[0]);
                return model;
            }
            else
            {
                return null;
            }
            
        }


        /// <summary>
        /// 获取最新库存盘点的Load编号
        /// </summary>
        /// <returns></returns>
        public string GetLoadNo_StartStorage()
        {
            string loadno = "XN";
            loadno = GetStartStorageNo(1, loadno);
            return loadno;
        }

        /// <summary>
        /// 获取最新盘库入库编号
        /// </summary>
        /// <returns></returns>
        public string GetInsNo_StartStorage()
        {
            string insno = "PR";
            insno = GetStartStorageNo(2, insno);
            return insno;
        }


        /// <summary>
        /// 获得盘点列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public DataTable  GetPDList(string gcity,string bcity)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@bcity", SqlDbType.NVarChar,20),
					new SqlParameter("@gcity", SqlDbType.NVarChar,25)};
            parameters[0].Value = bcity;
            parameters[1].Value = gcity;
            DataTable dt = new DataTable();

            SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_GetPDList", parameters);
            dt.Load(dr);
            dr.Close();

            return dt;
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="barcode"></param>
        /// <param name="baridarr"></param>
        /// <returns></returns>
        public DataTable GetDataList(string barcode, string arrbarid, out string barid)
        {
            DataTable dt = new DataTable();
            string strsql = "";
            
            if (barcode != null && barcode.Trim() !="")
            {
                strsql = "select id from BarCode where barcodevalue='" + barcode + "'";
                dt = DbHelperSQL.Query(strsql).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    if (arrbarid.Trim() != "")
                    {
                        arrbarid += ",";
                    }
                    arrbarid += dt.Rows[0][0].ToString();
                    for (int i = 1; i < dt.Rows.Count; i++)
                    {
                        arrbarid += ",";
                        arrbarid += dt.Rows[i][0].ToString();
                    }
                }
            }

            if (dt.Rows.Count > 0)
            {
                StringBuilder strSql = new StringBuilder();

                if (barcode != null)
                {
                    strSql.Append("select b.barcodevalue");
                    strSql.Append(" ,case LEFT(i.wrlno,1)");
                    strSql.Append(" when 'M'  then  '<a target=\"_blank\" href=\"/storage/Inwrl.aspx?insid=' + cast (i.id as varchar(10)) + '\">' + i.wrlno +'</a>' ");
                    strSql.Append(" else ");
                    strSql.Append("	    CASE LEFT(i.wrlno,2) ");
                    strSql.Append("     WHEN 'RS' THEN '<a target=\"_blank\" href=\"/storage/ProduceStorage.aspx?insid=' + cast (i.id as varchar(10)) + '\">' + i.wrlno +'</a>' ");
                    strSql.Append("     ELSE '<a target=\"_blank\" href=\"/storage/InStorage.aspx?insid=' + cast (i.id as varchar(10)) + '\">' + i.wrlno +'</a>' ");
                    strSql.Append("     END ");
                    strSql.Append("END wrlno");
                    strSql.Append(", case when o.outStorageNo is null ");
                    strSql.Append(" then ''");
                    strSql.Append(" else '<a target=\"_blank\" href=\"/Storage/outStorageUpdate.aspx?outid=' + cast (o.id as varchar(15)) + '\">' + o.outStorageNo +'</a>'");
                    strSql.Append("END outStorageNo");
                }
                else
                {
                    strSql.Append("select row_number()over(order by b.barcodevalue )as 序号");
                    strSql.Append(",b.barcodevalue as 条码");
                    strSql.Append(",i.wrlno as 入库单号");
                    strSql.Append(",o.outStorageNo as 出库单号");
                }

                strSql.Append(" from BarCode b");
                strSql.Append(" left join instorage i on b.InStrorageID=i.id ");
                strSql.Append(" left join OutStorage o on b.OutStrorageID=o.id");
                strSql.Append(" where  b.id in (" + arrbarid + ")");
                strSql.Append(" group by b.barcodevalue,i.wrlno,i.id,o.id,o.outStorageNo ");
                dt = DbHelperSQL.Query(strSql.ToString()).Tables[0];
            }
                barid = arrbarid;
            
            return dt;
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListByBarcode(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,loadnumid,cjoid,ParmentID,loadnumv,barcodevalue,goodscode,goodsname,remark,storagetype,storageid,status,gwt,tare1,tare2,nwt,qty,comeunder,packageno,isLocak,InStrorageID,OutStrorageID,StorageArea,Attribute,stroageNo,rubbishcode,rubbishtype,rdh,plateid,plateno,boxcount,createdate,isdel_out,sealno,barbelong,bargraphic,goodsid,outdate,insno,outsno,clearmonth,cumname,projectcode,projectname,cumshortname,bussCode,SType ");
            strSql.Append(" FROM BarCode ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
		#endregion  ExtensionMethod
	}
}
